const {readJson,writeJson} = require('../../until/fs');
const {HEALTH_JSON,USER_JSON} = require('../../config/config.data');
const uuid = require('node-uuid')

// 根据用户获取列表
let getHealthlist = (req,res) =>{

    let {user} = req.body
    let list = readJson(HEALTH_JSON);
        list = list.filter(item=>item.user === user).map(item=>{
            return {
                id:item.id,
                city:item.city,
                area:item.area,
                submitDate:item.submitDate,
                address:item.address
            }
        })
    res.send({code:1,msg:"健康列表数据请求成功",data:list})

}

// 根据id 获取详情

let getHealthDetail = (req,res)=> {
    const {id} = req.body;
  
    const list = readJson(HEALTH_JSON);

    let detail = list.find(item=>item.id===id)

    res.send({code:1,msg:"健康列表数据请求成功",data:detail})
}


// 数据提交

let setHealthlist = (req,res) =>{
    let {collectionData} = req.body
   
    let userData  = readJson(USER_JSON).find(item=>item.user === collectionData.user);
    
    const {sex,cardType,name,card,birthday} = userData

    collectionData = {...collectionData,sex,cardType,name,card,birthday}
    //生成health列表id
    collectionData.id = uuid.v1()

    let healthData = readJson(HEALTH_JSON)

    healthData.push(collectionData)

    writeJson(HEALTH_JSON,healthData)

    res.send({code:1,msg:"健康列表数据提交成功",data:readJson(HEALTH_JSON)})
}

module.exports = {
    getHealthlist,getHealthDetail,setHealthlist
}